package in.muton.edushare.components.tasks.invitations;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.wicket.converters.choicerenderers.GenericRenderer;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.HashMap;
import java.util.List;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.dao.UserDeptLinkDAO;
import com.edushare.common.dao.UserRolesDAO;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.Users;

public class ThirdPartyDeptInvite extends EdusharePanel {
	private static final long serialVersionUID = -7166105172292137814L;
	private IModel<Users> owner;
	private IModel<Departments> deptModel;
	private GenericRenderer<Integer> gr;

	public ThirdPartyDeptInvite(String id, IModel<Users> model,
			EduShareBasePage page) {
		super(id, model, page);
		owner = model;
		add(CSSPackageResource.getHeaderContribution(
				ThirdPartyCourseInvite.class, "Invitations.css"));
		Form<Void> f = new Form<Void>("dummyForm");
		add(f);
		f.add(new Label("userName", owner.getObject().getCompleteUserName()));
		f.add(new Button("studentInvite") {
			@Override
			public void onSubmit() {
				UserDeptLinkDAO udlDAO = new UserDeptLinkDAO(getORMInterface());
				getSelectedInst();
				setError(udlDAO.addUserFB(deptModel.getObject(), owner
						.getObject(), UserDeptLink.STUDENT, getLoggedInUser()));
			}

			private static final long serialVersionUID = 5089484037645395257L;
		});
		f.add(new Button("facInvite") {
			@Override
			public void onSubmit() {
				UserDeptLinkDAO udlDAO = new UserDeptLinkDAO(getORMInterface());
				getSelectedInst();
				setError(udlDAO.addUserFB(deptModel.getObject(), owner
						.getObject(), UserDeptLink.TEACHER, getLoggedInUser()));
			}

			private static final long serialVersionUID = 5089484037645395257L;
		});
		f.add(new Button("adminInvite") {
			@Override
			public void onSubmit() {
				UserDeptLinkDAO udlDAO = new UserDeptLinkDAO(getORMInterface());
				getSelectedInst();
				setError(udlDAO.addUserFB(deptModel.getObject(), owner
						.getObject(), UserDeptLink.PRIMARY_ADMIN,
						getLoggedInUser()));
			}

			private static final long serialVersionUID = 5089484037645395257L;
		});

		gr = new GenericRenderer<Integer>(getOwnedDepts(), null);
		f.add(new DropDownChoice<String>("deptSelect", gr.getModel(), gr
				.getOptions(), gr));
	}

	private HashMap<String, Integer> getOwnedDepts() {
		UserRolesDAO uDAO = new UserRolesDAO(getORMInterface());
		UserDeptLinkDAO udlDAO = new UserDeptLinkDAO(getORMInterface());
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		if (uDAO.isUserAdmin(getLoggedInUser())) {
			Universities univ = uDAO.getAdministeredUniv(getLoggedInUser());
			List<Departments> deptList = new DepartmentsDAO(getORMInterface())
					.getDeptForUniv(univ);
			for (Departments dept : deptList) {
				boolean isPresent = udlDAO.isUserPresent(dept, owner
						.getObject());
				if (!isPresent) {
					map.put(dept.getDeptName(), dept.getDepartmentId());
				}
			}
		}

		List<Departments> deptList = udlDAO.getDeptByUserRole(
				getLoggedInUser(), UserDeptLink.PRIMARY_ADMIN);
		for (Departments dept : deptList) {
			boolean isPresent = udlDAO.isUserPresent(dept, owner
					.getObject());
			if (!isPresent && map.get(dept.getDeptName())!=null) {
				map.put(dept.getDeptName(), dept.getDepartmentId());
			}
		}
		return map;
	}

	private void getSelectedInst() {
		if (gr.getSelection() == null) {
			setError("Please choose a department to invite "
					+ owner.getObject().getCompleteUserName() + "to");
			return;
		}
		int instId = gr.getSelection();
		Departments d = new DepartmentsDAO(getORMInterface())
				.getDepartmentByPk(instId);
		deptModel = new CayenneModel<Departments>(d);
	}

	@Override
	public boolean isVisible() {
		return getOwnedDepts().size() > 0
				&& getLoggedInUser() != owner.getObject();
	}
}
